package com.leyou.item.web;


import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;


    /**
     *   查询规格
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id") Long id){


        return ResponseEntity.ok(specService.queryGroupByCategory(id));

    }

    /**
     *  新增规格
     * @param specGroupDTO
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveGroupByCategory(@RequestBody SpecGroupDTO specGroupDTO){

        specService.saveGroupByCategory(specGroupDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *  删除规格
     * @param id
     * @return
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteGroupByCategory(@PathVariable("id") Long id){

        specService.deleteGroupByCategory(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格
     * @param specGroupDTO
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateGroupByCategory(@RequestBody SpecGroupDTO specGroupDTO){

        specService.updateGroupByCategory(specGroupDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询规格参数
     * @param gid 组id
     * @param cid 分类id
     * @param searching 是否用于搜索
     * @return 规格组集合
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching
            ){

        return ResponseEntity.ok(specService.querySpecParams(gid,cid,searching));

    }


    @PostMapping("param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){

        specService.saveParam(specParamDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){

        specService.updateParam(specParamDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteParam(@PathVariable("id")Long id){

        specService.deleteParam(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询规格参数组，及组内参数
     * @param id 商品分类id
     * @return 规格组及组内参数
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecsByCid(@RequestParam("id") Long id){

        return ResponseEntity.ok(specService.querySpecsByCid(id));
    }

}
